﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CuaHangMayTinh.BusinessEntities;
using CuaHangMayTinh.DataProvider;
using CuaHangMayTinh.Utilities;
using System.Data;

namespace CuaHangMayTinh.DataAccessLayer
{
    /// <summary>
    /// 
    /// </summary>
    public class BaoHanhSuaChuaDA
    {
        public BaoHanhSuaChuaDA()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Insert(BaoHanhSuaChuaEntity entity)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();

                pb.AddParameter("BH_SC", entity.BH_SC);
                pb.AddParameter("MaNV", entity.MaNV);
                pb.AddParameter("MaKH", entity.MaKH);
                pb.AddParameter("MaMH", entity.MaMH);
                pb.AddParameter("NgayLap", entity.NgayLap);
                pb.AddParameter("NgayHenTra", entity.NgayHenTra);
                pb.AddParameter("TinhTrangNhan", entity.TinhTrangNhan);
                pb.AddParameter("GhiChu", entity.GhiChu);

                return (int)DBFactory.Database.ExecuteNonQuery("BaoHanh_SuaChua_Insert", pb.Parameters);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="conn"></param>
        /// <param name="tran"></param>
        /// <returns></returns>
        int InsertTS(BaoHanhSuaChuaEntity entity, DbConnector conn, System.Data.Common.DbTransaction tran)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();

                pb.AddParameter("BH_SC", entity.BH_SC);
                pb.AddParameter("MaNV", entity.MaNV);
                pb.AddParameter("MaKH", entity.MaKH);
                pb.AddParameter("MaMH", entity.MaMH);
                pb.AddParameter("NgayLap", entity.NgayLap);
                pb.AddParameter("NgayHenTra", entity.NgayHenTra);
                pb.AddParameter("TinhTrangNhan", entity.TinhTrangNhan);
                pb.AddParameter("GhiChu", entity.GhiChu);

                return (int)DBFactory.Database.ExecuteNonQueryGetID("BaoHanh_SuaChua_Insert", pb.Parameters, conn, tran);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int InsertTS(BaoHanhSuaChuaEntity entity)
        {
            try
            {
                DbConnector conn = DBFactory.Database.GetConnector();
                System.Data.Common.DbTransaction tran = DBFactory.Database.CreateTransaction(conn);

                int IDBH = this.InsertTS(entity, conn, tran);
                if (IDBH == 0)
                    return 0;
                entity.mathang.TinhTrang = 2;

                entity.mathang.MaPhieuBaoHanhSuaChua = IDBH;

                if (!MatHangDA.UpDateBHSCTS(entity.mathang, conn, tran))
                    return 0;

                DBFactory.Database.CommitTransaction(conn, tran);
                return IDBH;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="conn"></param>
        /// <param name="tran"></param>
        /// <returns></returns>
        bool Update_DaTraHang(BaoHanhSuaChuaEntity entity, DbConnector conn, System.Data.Common.DbTransaction tran)
        {
            try
            {
                ParameterBuilder pb = DBFactory.CreateParamBuilder();

                pb.AddParameter("MaBaoHanh_SuaChua", entity.MaBaoHanh_SuaChua);
                pb.AddParameter("NgayTraHang", entity.NgayTraHang);
                pb.AddParameter("TinhTrangTra", entity.TinhTrangTra);
                pb.AddParameter("TongTien", entity.TongTien);
                pb.AddParameter("GhiChu", entity.GhiChu);
                int numRowEffected = DBFactory.Database.ExecuteNonQuery("BaoHanhSuaChua_UpDate_DaTraHang", pb.Parameters, conn, tran);
                return (numRowEffected > 0);
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Update_DaTraHang(BaoHanhSuaChuaEntity entity)
        {
            try
            {
                DbConnector conn = DBFactory.Database.GetConnector();
                System.Data.Common.DbTransaction tran = DBFactory.Database.CreateTransaction(conn);

                bool kq = this.Update_DaTraHang(entity, conn, tran);

                if (kq == false)
                    return kq;
                entity.mathang.TinhTrang = 1;
                kq = MatHangDA.UpDateBHSCTS(entity.mathang, conn, tran);

                DBFactory.Database.CommitTransaction(conn, tran);
                return kq;

            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="View"></param>
        /// <returns></returns>
        public DataTable Find_All(bool View)
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pb = DBFactory.CreateParamBuilder();
                pb.AddParameter("View", View);

                dt = DBFactory.Database.FillDataTable("BaoHanhSuaChua_FindAll", pb.Parameters);
                return dt;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                throw new Exception(ex.Message, ex);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public DataTable GetMaHDMax()
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();

                dt = DBFactory.Database.FillDataTable("BaoHanhSuaChua_GetMaHDMax", pd.Parameters);
                return dt;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="MaBHSC"></param>
        /// <returns></returns>
        public DataTable GetMatHang(int MaBHSC)
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("MaBaoHanhSuaChua", MaBHSC);

                dt = DBFactory.Database.FillDataTable("BaoHanhSuaChua_GetThongTinMatHang", pd.Parameters);
                return dt;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public DataTable GetInfoHoaDon(int MaBHSC)
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("MaBaoHanhSuaChua", MaBHSC);

                dt = DBFactory.Database.FillDataTable("BaoHanhSuaChua_GetInfo", pd.Parameters);
                return dt;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        DataTable GetInfoMaBaoHanhSuaChua_ID(int MaBaoHanhSuaChua)
        {
            try
            {
                DataTable dt = new DataTable();
                ParameterBuilder pd = DBFactory.CreateParamBuilder();
                pd.AddParameter("MaBaoHanhSuaChua", MaBaoHanhSuaChua);

                dt = DBFactory.Database.FillDataTable("BaoHanhSuaChua_Find_MaBaoHanhSuaChua", pd.Parameters);
                return dt;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return null;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="MaBaoHanhSuaChua"></param>
        /// <returns></returns>
        public BaoHanhSuaChuaEntity GetAllInfo_ID(int MaBaoHanhSuaChua)
        {
            try
            {
                List<BaoHanhSuaChuaEntity> baohanhs = ConvertToList(this.GetInfoMaBaoHanhSuaChua_ID(MaBaoHanhSuaChua));

                BaoHanhSuaChuaEntity baohanh = baohanhs[0];

                MatHangDA mathangda = new MatHangDA();

                List<MatHangEntity> mathangs = new List<MatHangEntity>();

                mathangs = mathangda.ConvertToList(mathangda.Find_MaPhieuBaoHanhSuaChua(MaBaoHanhSuaChua));

                baohanh.mathang = mathangs[0];

                return baohanh;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                return null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public List<BaoHanhSuaChuaEntity> ConvertToList(DataTable dt)
        {
            List<BaoHanhSuaChuaEntity> list = new List<BaoHanhSuaChuaEntity>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int ma = (int)dt.Rows[i][0];
                string mahd = dt.Rows[i][1].ToString();
                bool bh_Sc = (bool)dt.Rows[i][2];
                int manv = (int)dt.Rows[i][3];
                int makh = (int)dt.Rows[i][4];
                int mamh = (int)dt.Rows[i][5];
                DateTime ngaylap = (DateTime)dt.Rows[i][6];
                DateTime ngayhentra = (DateTime)dt.Rows[i][7];

                DateTime ngaytrahang = DateTime.Now;
                try
                {
                    ngaytrahang = (DateTime)dt.Rows[i][8];   
                }
                catch
                {
                    ngaytrahang = DateTime.Now;
                }

                string tttra = dt.Rows[i][9].ToString();
                string ttnhan = dt.Rows[i][10].ToString();
                bool dang_bh = (bool)dt.Rows[i][11];
                decimal tong;
                try
                {
                    tong = (decimal)dt.Rows[i][12];
                }
                catch (Exception)
                {
                    tong = 0;
                }
                
                string ghichu = dt.Rows[i][13].ToString();

                BaoHanhSuaChuaEntity a = new BaoHanhSuaChuaEntity(ma, mahd, bh_Sc, manv, makh, mamh, ngaylap, ngayhentra, ngaytrahang, ttnhan, tttra, dang_bh, tong, ghichu);
                list.Add(a);
            }
            return list;
        }
    }
}